package com.unitd.frame.sso.common.config;

import com.unitd.frame.comm.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;

/**
 * @desc SSO 的Properties 辅助工具类,用于SSO的全局属性配置信息的配置、读取、操作等
 * @filename SSOProperties.java
 * @copyright www.unitd.com
 * @author Hudan
 * @version 1.0
 * @date 2016/10/14
 */
public class SSOProperties {

	private static final Logger logger = LoggerFactory.getLogger(SSOProperties.class.getName());

	private final Properties properties;

	/**
	 * @desc 构造方法
	 */
	public SSOProperties(Properties properties) {
		this.properties = properties;
	}

	public SSOProperties(Properties mergeProperties, String runMode) {
		this.properties = extractRunMode(mergeProperties, runMode);
	}

	public SSOProperties(Properties mergeProperties, String runMode, String currentMode) {
		this.properties = extractRunMode(mergeProperties, runMode, currentMode);
	}

	/**
	 * @desc properties 提取当前模式配置(dev_mode 开发模式;test_mode 测试模式;online_mode 生产模式)
	 * eclipse/idea 开发模式配置: 在启动参数 Arguments 属性 VM arguments 中增加如下设置: -Dsso.run.mode=dev_mode
	 * @param prop    SSO属性配置对象
	 * @param runMode 当前运行模式的key值
	 * @return
	 */
	public static Properties extractRunMode(Properties prop, String runMode) {
		return extractRunMode(prop, runMode, "online_mode");
	}

	/**
	 * @desc properties 提取当前模式配置
	 * @param prop        配置文件 Properties
	 * @param runMode     当前运行模式的key值,默认为:sso.run.mode
	 * @param defaultMode 默认模式
	 * @return 当前模式配置属性
	 */
	public static Properties extractRunMode(Properties prop, String runMode, String defaultMode) {
		if (prop == null || StringUtils.isBlank(runMode) || StringUtils.isBlank(defaultMode)) {
			return null;
		}

		// 获取路由规则, 系统属性设置mode优先; 先取系统属性中的runMode,取不到或者为空,再取配置文件中的
		Properties properties = new Properties();
		String mode = System.getProperty(runMode);
		if (mode == null) {
			String str = prop.getProperty(runMode);
			mode = (str != null) ? str : defaultMode;
		}
		logger.info("sso.run.mode=" + mode);
		properties.put(runMode, mode);

		Set<Entry<Object, Object>> es = prop.entrySet();
		for (Entry<Object, Object> entry : es) {
			String key = (String) entry.getKey();
			int idx = key.lastIndexOf("_mode");
			String realKey = key;
			if (idx > 0) {
				if (key.contains(mode)) {
					realKey = key.substring(0, key.lastIndexOf("_" + mode));
				} else {
					realKey = null;
				}
			}
			if (realKey != null && !properties.containsKey(realKey)) {
				Object value;
				if (idx > 0) {
					value = prop.get(realKey + "_" + mode);
				} else {
					value = prop.get(realKey);
				}
				if (value != null) {
					properties.put(realKey, value);
				} else {
					throw new RuntimeException("property属性["+realKey+"]的值不能为空!");
				}
			}
		}
		return properties;
	}

	/**
	 * @desc 根据指定的键获取对应的属性值
	 * @param key 配置属性键
	 * @return String 属性值
	 */
	public String get(String key) {
		return properties.getProperty(key);
	}

	/**
	 * @desc 根据指定的键获取对应的属性值,如果值为空,则赋予默认值
	 * @param key 配置属性键
	 * @param defaultVal 默认值
	 * @return String 属性值
	 */
	public String get(String key, String defaultVal) {
		String val = this.get(key);
		return val == null ? defaultVal : val;
	}

	public String findValue(String... keys) {
		for (String key : keys) {
			String value = this.get(key);
			if (value != null) {
				return value;
			}
		}
		return null;
	}

	public boolean getBoolean(String key, boolean defaultVal) {
		String val = this.get(key);
		return val == null ? defaultVal : Boolean.parseBoolean(val);
	}

	public long getLong(String key, long defaultVal) {
		String val = this.get(key);
		return val == null ? defaultVal : Long.parseLong(val);
	}

	public int getInt(String key, int defaultVal) {
		return (int) getLong(key, defaultVal);
	}

	public double getDouble(String key, double defaultVal) {
		String val = this.get(key);
		return val == null ? defaultVal : Double.parseDouble(val);
	}

	public <T extends Enum<T>> T getEnum(String key, Class<T> type, T defaultValue) {
		String val = this.get(key);
		return val == null ? defaultValue : Enum.valueOf(type, val);
	}
}